package de.baumann.browser.present

import android.util.Log
import de.baumann.browser.api.OdinObserver
import de.baumann.browser.api.net.vo.NewsColumn
import de.baumann.browser.api.net.vo.Weather
import de.baumann.browser.base.BasePresenter
import de.baumann.browser.iview.IHomeView
import de.baumann.browser.model.CenterModel
import de.baumann.browser.model.NewsModel

class HomePresenter : BasePresenter<IHomeView>() {

    /**
     * 获取新闻栏目
     * 先从本地缓存读取数据
     * 再从网络更新本地缓存
     */
    fun getNewColumns() {
        NewsModel().column
                .subscribe(object : OdinObserver<List<NewsColumn>>() {
                    override fun onSuccess(data: List<NewsColumn>?) {
                        if (view != null && data != null)
                            view.setColumns(data as ArrayList<NewsColumn>)
                    }
                })
    }

    fun getWeather(city: String) {
        CenterModel().getWeather(city)
                .subscribe(object : OdinObserver<Weather>() {
                    override fun onSuccess(data: Weather?) {
                        if (view != null && data != null) {
                            view.setCity(city)
                            view.setCondTxt(data.condTxt)
                            view.setPm25(data.pm25)
                            view.setQlty(data.qlty)
                            view.setTmp(data.tmp)
                        }
                    }
                })
    }
}